﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using ZFS.Library;
using ZFS.Library.Helper;
using ZFS.Model;
using ZFSData;
using ZFSData.Manager;

namespace ZFS.ServerLibrary
{
    public partial class BaseService : IBaseService
    {

        #region IUserService

        /// <summary>
        ///  登录系统
        /// </summary>
        /// <param name="account">账户</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        public byte[] Login(string account, string password)
        {
            try
            {
                var User = Factory.GetUserManager().Login(account, password);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = User });
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("登录错误", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }

        /// <summary>
        /// 获取用户权限
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        public byte[] GetAuthority(string account)
        {
            try
            {
                var UserAuth = Factory.GetUserManager().GetAuthority(account);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = UserAuth });
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("获取用户权限", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }

        /// <summary>
        /// 根据账户获取账户信息
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        public byte[] GetModelByAccount(string account)
        {
            try
            {
                var User = Factory.GetUserManager().GetModelByAccount(account);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = User });
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("账户获取账户信息", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }

        /// <summary>
        /// 登出
        /// </summary>
        /// <param name="account"></param>
        public void Logout(string account)
        {
            try
            {
                Factory.GetUserManager().Logout(account);
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("登出", ex);
            }
        }

        public byte[] GetModelsByUser(byte[] search)
        {
            try
            {
                var UserSearch = ZipTools.DecompressionObject(search) as tb_User;
                var UserList = Factory.GetUserManager().GetModels(UserSearch);

                return ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = UserList });
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("查询用户数据", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }

        public byte[] GetPagingModelsByUser(int pageIndex, int pageSize, byte[] search, bool Asc = false)
        {
            try
            {
                int totalCount;
                var UserSearch = ZipTools.DecompressionObject(search) as tb_User;
                var UserList = Factory.GetUserManager().GetPagingModels(pageIndex, pageSize, out totalCount, UserSearch, Asc);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = UserList, TotalCount = totalCount });
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("查询用户数据-分页", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }

        public byte[] DeleteEntityByUser(byte[] entity)
        {
            try
            {
                var User = ZipTools.DecompressionObject(entity) as tb_User;
                bool result = Factory.GetUserManager().DeleteEntity(User);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = result });
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("删除用户", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }

        public byte[] UpdateEntityByUser(byte[] entity)
        {
            try
            {
                var User = ZipTools.DecompressionObject(entity) as tb_User;
                bool result = Factory.GetUserManager().UpdateEntity(User);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = result });

            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("更新用户", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }

        public byte[] AddEntityByUser(byte[] entity)
        {
            try
            {
                var User = ZipTools.DecompressionObject(entity) as tb_User;
                var NewUser = Factory.GetUserManager().AddEntity(User);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = NewUser });
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("新增用户", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }

        public byte[] ExistEntityByUser(byte[] entity)
        {
            try
            {
                var User = ZipTools.DecompressionObject(entity) as tb_User;
                bool result = Factory.GetUserManager().ExistEntity(User);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = true, Results = result });
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLog("检查用户是否存在", ex);
                return ZipTools.CompressionObject(new ServiceResponse() { Success = false, ErrorCode = "", Message = ex.Message });
            }
        }

        #endregion

    }
}
